package com.yj.cloudnote.mongo.dao.Impl;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.yj.cloudnote.mongo.dao.AbstractBaseMongoTemplete;
import com.yj.cloudnote.mongo.dao.FilesDao;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;

@Repository
public class FilesDaoImpl extends AbstractBaseMongoTemplete implements FilesDao{
    @Override
    public Boolean initFiles(DBObject dbObject) {
        mongoTemplate.insert(dbObject,"cn_files");
        return true;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 19:35 2018/4/27
     */
    @Override
    public Boolean insertOne(Map<String, Object> params) {
        DBObject dbObject = new BasicDBObject();
        dbObject.putAll(params);
        mongoTemplate.insert(dbObject,"cn_files");
        return true;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 22:18 2018/4/27
     */
    @Override
    public Boolean updataOne(Map<String, Object> where, Map<String, Object> set) {
//        DBObject DBwhere = new BasicDBObject();
//        DBwhere.putAll(where);
        BasicDBList andObj = new BasicDBList();
        for (Map.Entry<String, Object> entry : where.entrySet()) {
            andObj.add(new BasicDBObject(entry.getKey(),entry.getValue()));
        }
        DBObject DBwhere = new BasicDBObject("$and", andObj);
        DBObject DBset = new BasicDBObject();
        DBObject dbObject = new BasicDBObject();
        dbObject.putAll(set);
        DBset.put("$set",dbObject);
        mongoTemplate.getCollection("cn_files").update(DBwhere,DBset);
        return true;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 22:18 2018/4/27
     */
    @Override
    public DBObject getOne(Map<String, Object> query, Map<String, Object> key) {
        BasicDBList andObj = new BasicDBList();
        for (Map.Entry<String, Object> entry : query.entrySet()) {
            andObj.add(new BasicDBObject(entry.getKey(),entry.getValue()));
        }
        DBObject DBquery = new BasicDBObject("$and", andObj);
        DBObject DBkey = new BasicDBObject();
        DBkey.putAll(key);
        DBObject result = mongoTemplate.getCollection("cn_files").findOne(DBquery,DBkey);
        return result;
    }
}
